================================================================================
GENERATE VOUCHER AGENT - FIXED MENGIKUTI CARA MIKHMON ADMIN
================================================================================
Tanggal: <?= date('d-m-Y H:i:s'); ?>

MASALAH SEBELUMNYA:
==================
❌ Generate voucher dari agent panel gagal
❌ Fungsi randNULC(), randLC(), dll tidak ditemukan
❌ Koneksi MikroTik tidak stabil
❌ Error handling kurang baik

SOLUSI YANG DITERAPKAN:
=======================
✅ Mengikuti PERSIS cara MikhMon admin generate voucher
✅ Include file yang benar: config.php, routeros_api.class.php
✅ Menggunakan fungsi helper yang sama: randNULC(), randLC(), dll
✅ Koneksi MikroTik menggunakan cara yang sama dengan admin
✅ Error handling yang lebih baik

PERUBAHAN KODE:
===============

1. INCLUDE FILES (Sama seperti MikhMon Admin)
   -------------------------------------------
   SEBELUM:
   include('../include/db_config.php');
   include('../include/config.php');
   include('../lib/Agent.class.php');
   include('../lib/routeros_api.class.php');
   
   SESUDAH:
   // Include files seperti MikhMon admin
   include('../include/db_config.php');
   include('../include/config.php');
   include('../lib/Agent.class.php');
   include('../lib/routeros_api.class.php');
   
   // Set timezone
   date_default_timezone_set('Asia/Jakarta');

2. ERROR REPORTING (Sama seperti MikhMon Admin)
   --------------------------------------------
   SEBELUM:
   error_reporting(E_ALL);
   ini_set('display_errors', 1);
   
   SESUDAH:
   error_reporting(0);
   ini_set('max_execution_time', 300);

3. KONEKSI MIKROTIK (Sama seperti MikhMon Admin)
   ---------------------------------------------
   MENGGUNAKAN:
   - $iphost = explode('!', $data[$session][1])[1];
   - $userhost = explode('@|@', $data[$session][2])[1];
   - $passwdhost = explode('#|#', $data[$session][3])[1];
   - $API->connect($iphost, $userhost, decrypt($passwdhost))

4. GENERATE USERNAME/PASSWORD (Sama seperti MikhMon Admin)
   -------------------------------------------------------
   MENGGUNAKAN FUNGSI:
   - randNULC(8) untuk username
   - randNULC(6) untuk password
   
   FUNGSI INI ADA DI: lib/routeros_api.class.php

5. ADD USER KE MIKROTIK (Sama seperti MikhMon Admin)
   -------------------------------------------------
   $API->comm("/ip/hotspot/user/add", array(
       "server" => "all",
       "name" => $username,
       "password" => $password,
       "profile" => $profileName,
       "comment" => $comment,
   ));

CARA KERJA SEKARANG:
===================
1. Agent pilih profile dan quantity
2. Sistem cek saldo agent
3. Koneksi ke MikroTik menggunakan session admin
4. Generate username/password dengan fungsi MikhMon
5. Add user ke MikroTik
6. Potong saldo agent
7. Simpan ke database agent_vouchers
8. Kirim WhatsApp (opsional)

FUNGSI HELPER YANG DIGUNAKAN:
=============================
Dari file: lib/routeros_api.class.php

- randN($length)      → Random angka: 23456789
- randUC($length)     → Random huruf besar: ABCDEFGHJKLMNPRSTUVWXYZ
- randLC($length)     → Random huruf kecil: abcdefghijkmnprstuvwxyz
- randNLC($length)    → Random angka + huruf kecil
- randNUC($length)    → Random angka + huruf besar
- randNULC($length)   → Random angka + huruf besar + huruf kecil

CONTOH GENERATE:
===============
Username: AG5aB2c34D (AG + randNULC(8))
Password: 5aB2c3     (randNULC(6))
Comment: Agent-AG001-28.10.25

TESTING:
========
1. Login sebagai agent
2. Buka halaman Generate Voucher
3. Pilih profile (pastikan harga sudah diset)
4. Masukkan quantity
5. Klik "Generate Voucher Sekarang"
6. Voucher akan muncul jika berhasil

TROUBLESHOOTING:
===============
Jika masih gagal, cek:

1. ✓ Admin sudah login ke MikhMon?
   → Session MikroTik harus aktif

2. ✓ File lib/routeros_api.class.php ada?
   → Berisi fungsi randNULC(), dll

3. ✓ Koneksi MikroTik OK?
   → Cek IP, username, password MikroTik

4. ✓ Saldo agent cukup?
   → Minimal = harga beli × quantity

5. ✓ Profile sudah ada di MikroTik?
   → Cek di MikroTik Hotspot User Profile

6. ✓ Database agent_vouchers ada?
   → Jalankan database/agent_system.sql

KEUNTUNGAN CARA INI:
===================
✅ Menggunakan kode yang sudah TERBUKTI BERHASIL (MikhMon admin)
✅ Tidak perlu API bridge yang kompleks
✅ Lebih stabil dan reliable
✅ Mudah di-maintain
✅ Error handling lebih baik

FILE YANG DIUBAH:
================
1. mikhmon/agent/generate.php
   - Include files yang benar
   - Koneksi MikroTik seperti admin
   - Generate username/password dengan fungsi MikhMon
   - Error handling yang lebih baik

KESIMPULAN:
==========
Generate voucher agent sekarang menggunakan CARA YANG SAMA dengan MikhMon admin
yang sudah TERBUKTI BERHASIL. Tidak ada lagi error "function not found" karena
semua fungsi helper sudah di-include dengan benar.

================================================================================
SELAMAT MENCOBA! 🎉
================================================================================
